import { useSupported } from "@vueuse/core"
function useFile(options = {}) {
    const {
        dataType = "Text",
        onSuccess = () => void 0,
        onError = () => void 0,
    } = options;
    const isSupported = useSupported(() => window && "showSaveFilePicker" in window && "showOpenFilePicker" in window);
    const fileHandle = ref();
    const data = ref();
    const file = ref();
    const fileName = computed(() => {
        var _a, _b;
        return (_b = (_a = file.value) == null ? void 0 : _a.name) != null ? _b : "";
    });
    const fileMIME = computed(() => {
        var _a, _b;
        return (_b = (_a = file.value) == null ? void 0 : _a.type) != null ? _b : "";
    });
    const fileSize = computed(() => {
        var _a, _b;
        return (_b = (_a = file.value) == null ? void 0 : _a.size) != null ? _b : 0;
    });
    const fileLastModified = computed(() => {
        var _a, _b;
        return (_b = (_a = file.value) == null ? void 0 : _a.lastModified) != null ? _b : 0;
    });
    async function open(_options = {}) {
        try {
            if (!isSupported.value)
                return;
            const [handle] = await window.showOpenFilePicker({ ...toValue(options), ..._options });
            fileHandle.value = handle;
            await updateData();
            await onSuccess(file.value)
        } catch (e) {
            onError(e)
        }
    }
    async function create(_options = {}) {
        if (!isSupported.value)
            return;
        fileHandle.value = await window.showSaveFilePicker({ ...options, ..._options });
        data.value = void 0;
        await updateData();
    }
    async function save(_options = {}) {
        if (!isSupported.value)
            return;
        if (!fileHandle.value)
            return saveAs(_options);
        if (data.value) {
            const writableStream = await fileHandle.value.createWritable();
            await writableStream.write(data.value);
            await writableStream.close();
        }
        await updateFile();
    }
    async function saveAs(_options = {}) {
        if (!isSupported.value)
            return;
        fileHandle.value = await window.showSaveFilePicker({ ...options, ..._options });
        if (data.value) {
            const writableStream = await fileHandle.value.createWritable();
            await writableStream.write(data.value);
            await writableStream.close();
        }
        await updateFile();
    }
    async function updateFile() {
        var _a;
        file.value = await ((_a = fileHandle.value) == null ? void 0 : _a.getFile());
    }
    async function updateData() {
        var _a, _b;
        await updateFile();
        const type = toValue(dataType);
        if (type === "Text")
            data.value = await ((_a = file.value) == null ? void 0 : _a.text());
        else if (type === "ArrayBuffer")
            data.value = await ((_b = file.value) == null ? void 0 : _b.arrayBuffer());
        else if (type === "Blob")
            data.value = file.value;
    }
    watch(() => toValue(dataType), updateData);
    return {
        isSupported,
        data,
        file,
        fileName,
        fileMIME,
        fileSize,
        fileLastModified,
        open,
        create,
        save,
        saveAs,
        updateData
    };
}

export default useFile